package org.tp.framework.container;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.tp.framework.util.ClassUtils;
import org.tp.framework.util.StringUtils;

/**
 * 对象容器 保存所有标记对象
 * 
 * @author thinstop
 *
 */
public class BeanContainer {
	private static Map<String, Object> beanContainer = new ConcurrentHashMap<>();

	public static void init(String basePackageName) {
		Set<Class<?>> clazz = ClassUtils.initClassSet(basePackageName);
		for (Class<?> class1 : clazz) {
			beanContainer.put(getBeanName(class1), class1);
		}
	}

	public static <T> T getBean(Class<?> bean) {
		String beanName = getBeanName(bean);
		return getBean(beanName);
	}

	public static void addBeanContainer(String beanName, Object bean) {
		beanContainer.put(beanName, bean);
	}

	public static boolean containerBean(String beanName) {

		return beanContainer.containsKey(beanName);
	}

	public static String getBeanName(Class<?> clazz) {
		if (clazz == null) {
			return null;
		}
		return clazz.getSimpleName();
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String beanName) {
		if (StringUtils.isEmpty(beanName)) {
			return null;
		}
		return (T) beanContainer.get(beanName);
	}

	public static Collection<?> getBeans() {

		return beanContainer.values();
	}

}
